package com.suiwei.quartz.config;

import com.suiwei.quartz.job.Test;
import org.quartz.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author suiwei
 * @date 2022-11-06 21:46
 */
@Configuration
public class QuartzConfig {
    @Resource
    private Scheduler scheduler;
    /**
     * spring的上下文对象
     */
    @Resource
    private ApplicationContext applicationContext;
    /**
     * 01-开启任务
     */
    public void startJob(){
        try {
            openJob(scheduler);
            //启动任务
            scheduler.start();
            //让主线程暂时不关闭，方便查看效果
            TimeUnit.SECONDS.sleep(10);
            //pauseJob("job1","group1");
            //deleteJob("job1","group1");
            modifyJob("trigger1", "triggergroup1","0/1 * * * * ?");
            TimeUnit.SECONDS.sleep(10);
            System.out.println("修改了");
            //resumeJob("job1","group1");
            //TimeUnit.SECONDS.sleep(10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 开启一个任务
     * @param scheduler
     */
    public void openJob(Scheduler scheduler){
        try {
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("applicationContext",applicationContext);
            //1.创建一个JobDetail
            JobDetail jobDetail = JobBuilder.newJob(Test.class).usingJobData(jobDataMap).withIdentity("job1", "group1").build();
            //2.触发器表达式对象
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/4 * * * * ?");
            //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("30 25 16 * * ?");
            //3.准备一个触发器对象
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggergroup1")
                    .withSchedule(cronScheduleBuilder).build();
            //4.开始调度
            scheduler.scheduleJob(jobDetail,cronTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        } finally {

        }
    }
    /**
     * 暂停一个任务,不是真的暂停，暂停期间的任务，在恢复执行后，都会再执行一遍
     * quartz中的暂停默认有补偿机制，如果不想要这种机制的话，可以在创建cron表达式的时候，加一个属性，然后再加一堆配置，较复杂，一般不用
     * 生产中，可以直接停止，再重新启动
     */
    public void pauseJob(String name,String group) throws SchedulerException {
        //任务的标识类
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail==null){
            System.out.println("任务不存在");
        }else {
            scheduler.pauseJob(jobKey);
        }
    }
    /**
     * 恢复某个暂停的任务的执行
     */
    public void resumeJob(String name,String group)throws Exception{
        JobKey jobKey = new JobKey(name,group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (null!=jobDetail){
            scheduler.resumeJob(jobKey);
        }else {
            System.out.println("任务不存在");
        }
    }

    /**
     * 删除任务
     * @param name
     * @param group
     * @throws Exception
     */
    public void deleteJob(String name,String group)throws Exception{
        JobKey jobKey = new JobKey(name,group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (null!=jobDetail){
            scheduler.deleteJob(jobKey);
        }else {
            System.out.println("任务不存在");
        }
    }
    /**
     * 动态的修改cron表达式
     */
    public void modifyJob(String name,String group,String cron)throws Exception{
        TriggerKey triggerKey = new TriggerKey(name, group);
        Trigger trigger = scheduler.getTrigger(triggerKey);
        if (trigger instanceof CronTrigger){
            CronTrigger cronTrigger = (CronTrigger) trigger;
            String cronExpression = cronTrigger.getCronExpression();
            System.out.println("原来的cron表达式："+cronExpression);
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
           //新的触发器
            CronTrigger cronTriggerNew = TriggerBuilder.newTrigger().withIdentity(name, group).withSchedule(cronScheduleBuilder).build();
            scheduler.rescheduleJob(triggerKey,cronTriggerNew);

        }
    }

}
